﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using DevExpress.XtraEditors;

namespace SchoolManagementSystem.Forms
{
    public partial class frmShowDetailForm : DevExpress.XtraEditors.XtraForm
    {
        public frmShowDetailForm()
        {
            InitializeComponent();
        }
        public frmShowDetailForm(DetailUC uc)
        {
            InitializeComponent();
            this._uc = uc;
        }
        public long ID { get; set; }
        System.Windows.Forms.Control _control;

        public System.Windows.Forms.Control Control1
        {
            get { return _control; }
            set { _control = value; }
        }
        private bool _parentClosed = false;
        private Controls.DetailUC _uc;

        public Controls.DetailUC UC { get { return _uc; } set { _uc = value; } }

        internal void CloseMe()
        {
            _parentClosed = true;
            this.Close();
        }
        public void ShowDock(Form mdiParent, System.Windows.Forms.Control control)
        {
            _control = control;


            if (control == null)
            {
                control = this._uc;
                _uc.Display();
            }

            this.Size = control.Size;

            this.Controls.Add(control);
            control.Dock = DockStyle.Fill;

            try
            {
                this.Text = ((DetailUC)control).GetTitleContext();
            }
            catch (Exception)
            {
                this.Text = control.Name;
            }

            if (mdiParent == null)
                this.ShowDialog();
            else
            {
                this.MdiParent = mdiParent;
                this.Show();
            }

            if (_uc != null)
                ID = _uc.ID;
        }

        private void frmShowDetailForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F11)
                this.Close();
            if (e.KeyCode == Keys.F1)
            {
              //show help form

            }
        }

        private void frmShowDetailForm_FormClosing(object sender, FormClosingEventArgs e)
        {
             try
            {
                _uc = (DetailUC)_control;

                if (_uc != null && _uc.SaveAble)
                {
                    System.Windows.Forms.DialogResult dlg = XtraMessageBox.Show(String.Format("Dữ liệu của {0} đã thay đổi, bạn có muốn lưu lại", _uc.ObjectName, _uc), String.Format("Lưu {0}", _uc.ObjectName), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                    if (dlg == System.Windows.Forms.DialogResult.Yes)
                        _uc.DetailSave();
                    else if (dlg == System.Windows.Forms.DialogResult.Cancel)
                    {
                        e.Cancel = true;
                    }
                }
            }
            catch (Exception)
            {
                
                //throw;
            }
        
        }
    }
}
